import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;

import javax.swing.*;

/**
 * The main program that runs at start up. 
 * Presents users with difficulty selection.
 */
public class MainMenu extends JFrame
{
	private GridBagConstraints gridBagConstraints;
	
	public static void main(String[] args)
	{
		MainMenu mainMenu = new MainMenu("Select a difficulty");
	}
	
	/**
	 * Assigns a title to the program
	 * @param title bar label
	 */
	public MainMenu(String title)
	{
		super(title);
		initialiseFrame();
	}
	
	/**
	 * Initialise the display for the user. Includes button placements.
	 * Contains buttons for selection and their respective messages.
	 */
	public void initialiseFrame()
	{
		this.getContentPane().removeAll();
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new GridBagLayout());
		gridBagConstraints = new GridBagConstraints();
		
		JButton easyButton = new JButton("Easy");
		JButton medButton = new JButton("Medium");
		JButton hardButton = new JButton("Hard");
		JButton veryHardButton = new JButton("Very Hard");
		
		gridBagConstraints.insets = new Insets(10, 10, 10, 10);
		gridBagConstraints.fill = GridBagConstraints.BOTH;
		gridBagConstraints.anchor = GridBagConstraints.CENTER;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.gridx = 0;
		
		this.add(easyButton, gridBagConstraints);
		easyButton.addActionListener(new ActionListener() {
			
			/* 
			 * Passes the creation of a new game with selected difficulty of Easy.
			 */
			@Override
			public void actionPerformed(ActionEvent e)
			{
				difficultyButton_Pressed(Difficulty.Easy);
			}
		});
		
		gridBagConstraints.gridy = 1;
		this.add(medButton, gridBagConstraints);
		medButton.addActionListener(new ActionListener() {
			
			/* 
			 * Passes the creation of a new game with selected difficulty of Medium.
			 */
			@Override
			public void actionPerformed(ActionEvent e)
			{
				difficultyButton_Pressed(Difficulty.Medium);
			}
		});
		
		gridBagConstraints.gridy = 2;
		this.add(hardButton, gridBagConstraints);
		hardButton.addActionListener(new ActionListener() {
			
			/* 
			 * Passes the creation of a new game with selected difficulty of Hard.
			 */
			@Override
			public void actionPerformed(ActionEvent e)
			{
				difficultyButton_Pressed(Difficulty.Hard);
			}
		});
		gridBagConstraints.gridy = 3;
		this.add(veryHardButton, gridBagConstraints);
		veryHardButton.addActionListener(new ActionListener() {
			
			/* 
			 * Passes the creation of a new game with selected difficulty of Very Hard.
			 */
			@Override
			public void actionPerformed(ActionEvent e)
			{
				difficultyButton_Pressed(Difficulty.VeryHard);
			}
		});

		this.pack();
		this.setSize(300, 300);
		this.setVisible(true);
		
		this.pack();
		this.setSize(300, 300);
		this.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - getWidth()) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - getHeight()) / 2);
		this.setVisible(true);
	}
	
	/**
	 * Creates a new Sudoku game based on selected difficulty setting.
	 * @param difficulty Difficulty setting
	 */
	private void difficultyButton_Pressed(Difficulty difficulty)
	{
		SudokuFrontEnd sp = new SudokuFrontEnd("Sudoku", difficulty);
		sp.addWindowListener(new WindowListener() {
			
			
			@Override
			public void windowClosed(WindowEvent arg0) {
			}

			@Override
			public void windowActivated(WindowEvent e) {
			}

			@Override
			public void windowClosing(WindowEvent e) {
				singlePlayerFrontEnd_Closed();
			}

			@Override
			public void windowDeactivated(WindowEvent e) {
			}

			@Override
			public void windowDeiconified(WindowEvent e) {
			}

			@Override
			public void windowIconified(WindowEvent e) {
			}

			@Override
			public void windowOpened(WindowEvent e) {
			}
		});
		this.setVisible(false);
	}
	
	private void singlePlayerFrontEnd_Closed()
	{
		this.setVisible(true);
		initialiseFrame();
	}
}
